function syncPlayers(mainPlayer, audioPlayer) {
	mainPlayer.pause();
	audioPlayer.pause();
	
	var ready = false;
	audioPlayer.addEventListener("canplay", function () {
		ready = true;
		mainPlayer.play();
	});
	mainPlayer.addEventListener("canplay", function () {
		if (ready) {
			audioPlayer.play();		
			ready = true;
		}
	});
	mainPlayer.addEventListener("play", function() {
		audioPlayer.play();	
		syncVideos(ready, mainPlayer, audioPlayer);
	});
	mainPlayer.addEventListener("pause", function () {
		audioPlayer.pause();
		syncVideos(ready, mainPlayer, audioPlayer);
	});
	mainPlayer.addEventListener("timeupdate", function () {
		syncVideos(ready, mainPlayer, audioPlayer);
	});
	audioPlayer.addEventListener("timeupdate", function () {
		//syncVideos(ready, audioPlayer, mainPlayer);
		syncVideos(ready, mainPlayer, audioPlayer);
	});
	mainPlayer.addEventListener("seeking", function () {
		syncVideos(ready, mainPlayer, audioPlayer);
		if (desyncHandle != 5) {
			audioPlayer.pause();		
		}
	});
	mainPlayer.addEventListener("seeked", function () {
		audioPlayer.play();
		syncVideos(ready, mainPlayer, audioPlayer);
	});
	audioPlayer.addEventListener("seeking", function () {
		syncVideos(ready, mainPlayer, audioPlayer);
		if (desyncHandle != -5) {
			mainPlayer.pause();		
		}
	});
	audioPlayer.addEventListener("seeked", function () {
		mainPlayer.play();
		syncVideos(ready, mainPlayer, audioPlayer);
	});
}		

//desyncHandle
function syncVideos0(ready, mainPlayer, audioPlayer) {
	if (ready) {
		if (audioPlayer.currentTime - mainPlayer.currentTime >= 0.6 ||
		 audioPlayer.currentTime - mainPlayer.currentTime <= -0.6) {
			audioPlayer.currentTime = mainPlayer.currentTime;		
		}
	}
}

function syncVideos1(ready, mainPlayer, audioPlayer) {
	if (ready) {
		if (audioPlayer.currentTime - mainPlayer.currentTime >= 1 ||
		 audioPlayer.currentTime - mainPlayer.currentTime <= -1) {
			audioPlayer.currentTime = mainPlayer.currentTime;		
		}
	}
}

function syncVideos2(ready, mainPlayer, audioPlayer) {
	if (ready) {
		if (audioPlayer.currentTime - mainPlayer.currentTime >= 500 ||
		 audioPlayer.currentTime - mainPlayer.currentTime <= -500) {
			audioPlayer.currentTime = mainPlayer.currentTime;		
		}
	}
}

function syncVideos3(ready, mainPlayer, audioPlayer) {
	if (ready) {
		if (audioPlayer.currentTime - mainPlayer.currentTime >= 0.45 ||
		 audioPlayer.currentTime - mainPlayer.currentTime <= -0.45) {
			audioPlayer.currentTime = mainPlayer.currentTime;		
		}
	}
}

function syncVideosPerfect(ready, mainPlayer, audioPlayer) {
	if (ready) {
		//audioPlayer.currentTime = mainPlayer.currentTime;
		// previous values: 0.1, -0.1
		if (audioPlayer.currentTime - mainPlayer.currentTime >= 0.12 ||
		 audioPlayer.currentTime - mainPlayer.currentTime <= -0.12) {
			audioPlayer.currentTime = mainPlayer.currentTime;		
		}
	}
}

function syncVideosLite(ready, mainPlayer, audioPlayer) {
	if (ready) {
		//audioPlayer.currentTime = mainPlayer.currentTime;
		// previous values: 0.1, -0.1
		if (audioPlayer.currentTime - mainPlayer.currentTime >= 0.22 ||
		 audioPlayer.currentTime - mainPlayer.currentTime <= -0.22) {
			audioPlayer.currentTime = mainPlayer.currentTime;		
		}
	}
}

function syncVideos(ready, mainPlayer, audioPlayer) {
	if (desyncHandle == -1) {
		return;	
	}
	if (debugSync) {
		var v = (mainPlayer.currentTime - audioPlayer.currentTime);
		if (v < 0) {
			v = v / -1;		
		}
		v *= 1000;
		document.getElementById("__debugOverlay_desync").innerHTML = v + " ms";
		//document.getElementById("video_debug_desync").innerHTML = "nPlayer.desync: " + 
		//	(v * 1000) + " ms [" + v + " s]";
	}
	if (desyncHandle == 0) {
		syncVideos0(ready, mainPlayer, audioPlayer);
	}else if (desyncHandle == 1) {
		syncVideos1(ready, mainPlayer, audioPlayer);
	}else if (desyncHandle == 2) {
		syncVideos2(ready, mainPlayer, audioPlayer);	
	}else if (desyncHandle == 3) {
		syncVideos3(ready, mainPlayer, audioPlayer);	
	}else if (desyncHandle == 5) {
		syncVideosPerfect(ready, mainPlayer, audioPlayer);
	}else if (desyncHandle == 6) {
		syncVideosLite(ready, mainPlayer, audioPlayer);
	}else {
		syncVideos0(ready, mainPlayer, audioPlayer);
	}	
}

/*
function syncVideos(ready, mainPlayer, audioPlayer) {
	//document.getElementById("video_debug_desync").innerHTML = "Desync: " + (audioPlayer.currentTime - mainPlayer.currentTime) + "ms"
	if (ready) {
		if (allowExtendedDesync && (audioPlayer.currentTime - mainPlayer.currentTime >= 500.0 || audioPlayer.currentTime - mainPlayer.currentTime <= -500.0)) {
			audioPlayer.currentTime = mainPlayer.currentTime;
		}else if (audioPlayer.currentTime - mainPlayer.currentTime >= 0.6 || audioPlayer.currentTime - mainPlayer.currentTime <= -0.6) {
			audioPlayer.currentTime = mainPlayer.currentTime;		
		}
	}
}*/
